package com.ruoyi.rsikManage.service.Impl;

import com.ruoyi.electricity.domain.DTO.HourlyData;
import com.ruoyi.rsikManage.domain.AlarmEvent;
import com.ruoyi.rsikManage.domain.AlarmEventDto;
import com.ruoyi.rsikManage.domain.MonitorPoint;
import com.ruoyi.rsikManage.domain.vo.AlarmUrlVo;
import com.ruoyi.rsikManage.domain.vo.AlarmtoSiteVo;
import com.ruoyi.rsikManage.domain.vo.ChartVo;
import com.ruoyi.rsikManage.mapper.AlarmEventMapper;
import com.ruoyi.rsikManage.mapper.MonitorPointMapper;
import com.ruoyi.rsikManage.service.AlarmEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@SuppressWarnings("all")
public class AlarmEventServiceImpl implements AlarmEventService {
    @Autowired
    private AlarmEventMapper alarmEventMapper;

    @Autowired
    private MonitorPointMapper monitorPointMapper;


    @Override
    public List<AlarmEvent> selectPageAlarmEvent(AlarmEvent alarmEvent) {
        return alarmEventMapper.selectPageAlarmEvent(alarmEvent);
    }

    @Override
    public List<AlarmEvent> selectCondition(AlarmEvent alarmEvent) {

        return alarmEventMapper.selectCondition(alarmEvent);
    }


    @Override
    public List<AlarmEvent> selectAll(Date startTime, Date endTime) {
        return alarmEventMapper.selectAll(startTime, endTime);
    }


    @Override
    public List<Map> selectChildNum() {
        return alarmEventMapper.selectChildNum();
    }

    @Override
    public String selectDictContent(String s, String alarmLevel) {
        return alarmEventMapper.selectDictContent(s, alarmLevel);
    }

    @Override
    public Map<String, Object> selectApproveCount(String startTime, String endTime, String strFori) {
        return alarmEventMapper.selectApproveCount(startTime, endTime, strFori);
    }

    @Override
    public int selectByApproveSize() {
        return alarmEventMapper.selectByApproveSize();
    }

    @Override
    public Map<String, Object> selectByApproveValue(String approve) {
        return alarmEventMapper.selectByApproveValue(approve);
    }

    @Override
    public List<ChartVo> selectDateCount(String startTime, String endTime, String s) {
        return alarmEventMapper.selectDateCount(startTime, endTime, s);
    }

    @Override
    public Map<String, Object> selectPointNum() {
        return alarmEventMapper.selectPointNum();
    }

    @Override
    public List<String> selectAlarmTypeName() {
        return alarmEventMapper.selectAlarmTypeName();
    }

    @Override
    public int selectNum(String level) {
        return alarmEventMapper.selectNum(level);
    }

    @Override
    public Map<String, Object> selectCountDay() {
        return alarmEventMapper.selectCountDay();
    }

    @Override
    public Map<String, Object> selectChildContent(String s) {
        return alarmEventMapper.selectChildContent(s);
    }

    @Override
    public Map<String, Object> selectTypeAndName(int i, String s) {
        return alarmEventMapper.selectTypeAndName(i, s);
    }

    @Override
    public List<Map> selectUrlImage(String name) {
        return alarmEventMapper.selectUrlImage(name);
    }

    @Override
    public Map<String, Object> selectCountLeft(String name) {
        return alarmEventMapper.selectCountLeft(name);
    }

    @Override
    public List<AlarmtoSiteVo> selectStandsPlace() {
        return alarmEventMapper.selectStandsPlace();
    }

    @Override
    public List<AlarmtoSiteVo> selectByid(String id) {
        return alarmEventMapper.selectByid(id);
    }

    @Override
    public List<AlarmtoSiteVo> selectByDeviceId(String startTime, String endTime, String monitorId) {
        return alarmEventMapper.selectByDeviceId(startTime, endTime, monitorId);
    }

    @Override
    public List<String> selectByNames(String s) {
        return alarmEventMapper.selectByNames(s);
    }

    @Override
    public List<AlarmUrlVo> selectNumByName() {
        return alarmEventMapper.selectNumByName();
    }

    @Override
    public List<String> selectByName() {
        return alarmEventMapper.selectByName();
    }

    @Override
    public List<AlarmUrlVo> selectFifteenDayData(String s1, String s) {
        return alarmEventMapper.selectFifteenDayData(s1, s);
    }


    @Override
    public List<AlarmtoSiteVo> selectBymonitorIds(List<String> stringList) {
        return alarmEventMapper.selectBymonitorIds(stringList);
    }

    @Override
    public List<AlarmtoSiteVo> selectByDeviceIds(String startTime, String endTime, List<String> stringList) {
        return alarmEventMapper.selectByDeviceIds(startTime, endTime, stringList);
    }

    @Override
    public List<AlarmtoSiteVo> selectByDeviceIdsForUnitCodes(LocalDateTime startTime, LocalDateTime endTime,
                                                             List<String> stringList,List<Long> unitCodes) {
        return alarmEventMapper.selectByDeviceIdsForUnitCodes(startTime, endTime, stringList,unitCodes);
    }

    @Override
    public List<AlarmUrlVo> selectSevenDayData(String s1, String s) {
        return alarmEventMapper.selectSevenDayData(s1, s);
    }

    @Override
    public List<AlarmUrlVo> selectUrls(String s1, String s) {
        return alarmEventMapper.selectUrls(s1, s);
    }


    @Override
    public List<HourlyData> getHourlyDataQuery1(Date startTime, Date endTime) {
//        List<HourlyData> list = alarmEventMapper.getHourlyDataQuery1(startTime, endTime);
//        // 封装返回来的小时
//        ArrayList<Integer> dateForList = new ArrayList<>();
//        for (HourlyData data : list) {
//            dateForList.add(data.getHour());
//        }
//        // 创建一个List用于存储所有的HourlyData对象
//        List<HourlyData> result = new ArrayList<>();
//        // 获取当前时间
//        LocalDateTime now = LocalDateTime.now();
//        // 创建一个格式化器，指定小时的格式为两位数
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
//        // 格式化当前时间并获取小时数
//        String hourStr = now.format(formatter);
//        int hour = Integer.parseInt(hourStr);
//        for (int i = hour; i > 0; i--) {
//            // 如果dateForList中不包含当前小时，则创建新的HourlyData对象并赋值
//            if (!dateForList.contains(i)) {
//                HourlyData hourlyData = new HourlyData();
//                hourlyData.setHour(i);
//                hourlyData.setTotalCount(0);
//                result.add(hourlyData);
//            } else {
//                // 如果dateForList中包含当前小时，则将其添加到result中
//                for (HourlyData data : list) {
//                    if (data.getHour() == i) {
//                        result.add(data);
//                        break;
//                    }
//                }
//            }
//        }
        return null;
    }

    @Override
    public List<HourlyData> getHourlyDataQuery(Date startTime, Date endTime) {
//        List<HourlyData> list = alarmEventMapper.getHourlyDataQuery(startTime, endTime);
//        // 封装返回来的小时
//        ArrayList<Integer> dateForList = new ArrayList<>();
//        for (HourlyData data : list) {
//            dateForList.add(data.getHour());
//        }
//        // 创建一个List用于存储所有的HourlyData对象
//        List<HourlyData> result = new ArrayList<>();
//        // 获取当前时间
//        LocalDateTime now = LocalDateTime.now();
//        // 创建一个格式化器，指定小时的格式为两位数
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
//        // 格式化当前时间并获取小时数
//        String hourStr = now.format(formatter);
//        int hour = Integer.parseInt(hourStr);
//        for (int i = hour; i > 0; i--) {
//            // 如果dateForList中不包含当前小时，则创建新的HourlyData对象并赋值
//            if (!dateForList.contains(i)) {
//                HourlyData hourlyData = new HourlyData();
//                hourlyData.setHour(i);
//                hourlyData.setTotalCount(0);
//                result.add(hourlyData);
//            } else {
//                // 如果dateForList中包含当前小时，则将其添加到result中
//                for (HourlyData data : list) {
//                    if (data.getHour() == i) {
//                        result.add(data);
//                        break;
//                    }
//                }
//            }
//        }

        return null;
    }

    @Override
    public List<AlarmUrlVo> selectThreeDayData(String s1, String s) {
        return alarmEventMapper.selectThreeDayData(s1, s);
    }

    @Override
    public List<Map> selectByType() {
        return alarmEventMapper.selectByType();
    }

    @Override
    public List<String> selectType() {
        return alarmEventMapper.selectType();
    }

    @Override
    public List<AlarmUrlVo> selectFiveTypes(String s, String s1) {
        return alarmEventMapper.selectFiveTypes(s, s1);
    }

    @Override
    public List<String> selectLevel() {
        return alarmEventMapper.selectLevel();
    }

    @Override
    public List<AlarmUrlVo> selectName() {
        return alarmEventMapper.selectName();
    }

    @Override
    public List<AlarmtoSiteVo> selectByDicValue(String dicValue) {
        return alarmEventMapper.selectByDicValue(dicValue);
    }

    @Override
    public List<String> selectStandMontior(String ids) {
        return alarmEventMapper.selectStandMontior(ids);
    }

    @Override
    public int selectDictSize(String s) {
        return alarmEventMapper.selectDictSize(s);
    }

    @Override
    public Map<String, Object> selectEveryTypeNum(String startTime, String endTime, int i) {
        return alarmEventMapper.selectEveryTypeNum(startTime, endTime, i);
    }

    @Override
    public Map<String, Object> selectEveryTypeNumForUnitCodes(LocalDateTime startTime, LocalDateTime endTime, int i,List<Long> unitCodes) {
        return alarmEventMapper.selectEveryTypeNumForUnitCodes(startTime, endTime, i,unitCodes);
    }

    @Override
    public List<Map> selectAlarmNumCount(String startTime, String endTime) {
        return alarmEventMapper.selectAlarmNumCount(startTime, endTime);
    }

    @Override
    public List<Map> selectAlarmNumCountV2(LocalDateTime startTime, LocalDateTime endTime,List<Long> unitCodes) {
        return alarmEventMapper.selectAlarmNumCountV2(startTime, endTime,unitCodes);
    }

    @Override
    public List<AlarmEvent> selectByIds(List<String> apeList) {
        return alarmEventMapper.selectByIds(apeList);
    }

    @Override
    public List<Map> selectAlarmEventByTimeandApeId(String apeId, Date startTime1, Date endTime1) {
        return alarmEventMapper.selectAlarmEventByTimeandApeId(apeId, startTime1, endTime1);
    }

    @Override
    public List<AlarmEventDto> selectApproal(Date startTime, Date endTime) {
        return alarmEventMapper.selectApproal(startTime, endTime);
    }

    @Override
    public int updateDispose(Map<String, Object> param) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        param.put("alarmTime", df.format(System.currentTimeMillis()));
        return alarmEventMapper.updateDispose(param);
    }

    @Override
    public int deleteAlarmEvent(Long[] ids) {
        return alarmEventMapper.deleteAlarmEvent(ids);
    }

    @Override
    public int deleteAlarmEvent2(Long[] ids) {
        return alarmEventMapper.deleteAlarmEvent2(ids);
    }

    @Override
    public boolean updateAlarmEvent(Map<String, Object> param) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        param.put("alarmTime", df.format(System.currentTimeMillis()));
        return alarmEventMapper.updateAlarmEvent(param);
    }

    @Override
    public void insertAlarmEvent(HashMap<String, Object> param) {
        alarmEventMapper.insertAlarmEvent(param);
    }

    @Override
    public int insertAlarm(AlarmEvent alarmEvent) {
        //判断是否有ip 有则DeviceId 拿ip赋值
        if (alarmEvent.getIP()!=null){
            String ip = alarmEvent.getIP();
            alarmEvent.setDeviceId(convertIpToNumeric(ip));
            MonitorPoint monitorPoint = new MonitorPoint();
            List<MonitorPoint> monitorPoints = monitorPointMapper.selectAllMonitorPoints2(monitorPoint.setResourceType(ip));
            String ownerApsName = null;
            if (!monitorPoints.isEmpty()) {
                ownerApsName = monitorPoints.get(0).getOwnerApsName();
            }
            alarmEvent.setOrgName(ownerApsName);
        }

        return  alarmEventMapper.insertAlarm(alarmEvent);
    }

    @Override
    public int insertAlarmBatch(List<AlarmEvent> alarmEvents) {
        //判断是否有ip 有则DeviceId 拿ip赋值
        if (alarmEvents.get(0).getIP()!=null){
            for (AlarmEvent alarmEvent :alarmEvents ){
                String ip = alarmEvent.getIP();
                alarmEvent.setDeviceId(convertIpToNumeric(ip));
                MonitorPoint monitorPoint = new MonitorPoint();
                List<MonitorPoint> monitorPoints = monitorPointMapper.selectAllMonitorPoints2(monitorPoint.setResourceType(ip));
                String ownerApsName = null;
                if (!monitorPoints.isEmpty()) {
                    ownerApsName = monitorPoints.get(0).getOwnerApsName();
                }
                alarmEvent.setOrgName(ownerApsName);
            }

        }

        return  alarmEventMapper.insertAlarmBatch(alarmEvents);
    }
    public static String convertIpToNumeric(String ip) {
        String[] parts = ip.split("\\."); // 使用正则表达式split，以点分隔IP地址
        StringBuilder sb = new StringBuilder();
        for (String part : parts) {
            // Integer.parseInt将字符串转换为整数，然后toString转回字符串形式，format确保至少有3位数，不够的前面补0
            sb.append(String.format("%03d", Integer.parseInt(part)));
        }
        return sb.toString();
    }
//
}
